﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TeamWork_Server.HTTP
{
    public class FileTransfer
    {
        HttpService service;
        TWServerConfiguration config;
        CodeUpdateService cus;

        public FileTransfer(string ip, int port, TWServerConfiguration sconfig)
        {
            service = new HttpService(ip, port);
            config = sconfig;
            cus = new CodeUpdateService();
            service.HttpContextReceived += service_HttpContextReceived;
            service.HttpContextReceived += cus.service_HttpContextReceived;
        }

        private void service_HttpContextReceived(object sender, HttpContextReceivedEventArgs e)
        {
            int d = new Random().Next();
            Log.Info("Received HTTP Context, ID: " + d.ToString("X") + ".");
            try
            {
                StreamReader reader = new StreamReader(e.Context.Request.InputStream);
                string l = reader.ReadLine();
                if (e.Context.Request.HttpMethod == "POST")
                {
                    if (l.Contains("/download"))
                    {
                        string fname = l.Replace("/download ", "");
                        Log.Info(fname);
                        StreamWriter writer = new StreamWriter(e.Context.Response.OutputStream);
                        /*e.Context.Response.ContentLength64 = Encoding.ASCII.GetByteCount(config.GetFileText(fname));
                        e.Context.Response.AddHeader("Content_Length", e.Context.Response.ContentLength64.ToString());*/
                        e.Context.Response.SendChunked = true;
                        writer.WriteLine(config.GetFileText(fname));
                        writer.Flush();
                        Log.Info("Sent file '" + fname + "' to a client.");
                        writer.Close();
                        reader.Close();
                    }

                }
                else
                {
                    if (e.Context.Request.Headers["packet_type"] == "GetFiles")
                    {
                        StreamWriter writer = new StreamWriter(e.Context.Response.OutputStream);
                        writer.AutoFlush = false;
                        string data = "";
                        foreach (string n in config.Files)
                        {
                            data += config.GetFileName(n) + ";";
                        }
                        writer.WriteLine(data);
                        writer.Flush();
                        writer.Close();
                        Log.Info("Sending File Names to client.");
                    }
                }
                Log.Info("Handling completed, ID: " + d.ToString("X"));
            }
            catch (Exception ex) {
                Log.Error("HTTP Context is invalid, ID: " + d.ToString("X") + "." + 
                    "Error-String: " + ex.ToString());
            }
        }

        public void Start()
        {
            service.Start();
            Log.Debug("File Transfer System Is Online");
        }

        public void Pause()
        {
            service.Pause();
            Log.Debug("File Transfer System Is Offline");
        }
    }
}
